package cn.cdeden.system.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.redis.utils.RedisUtils;
import cn.cdeden.system.domain.dto.CoreConfigDto;
import cn.cdeden.system.domain.enums.CoreRedisKeyEnum;
import cn.cdeden.system.utils.RedisNewUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cdeden.system.domain.CoreConfig;
import cn.cdeden.system.mapper.CoreConfigMapper;
import cn.cdeden.system.service.CoreConfigService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
@CacheConfig(cacheNames = CoreConfigService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
//@RequiredArgsConstructor
public class CoreConfigServiceImpl extends ServiceImpl<CoreConfigMapper, CoreConfig> implements CoreConfigService{

    private final RedisNewUtils redisUtils;

    private final String redisCoreConfig = CoreRedisKeyEnum.CORE_CONFIG.getType();

    @Override
    public CoreConfigDto getByConfigCode(String configCode) throws NullPointerException {
        if (StringUtils.isEmpty(configCode)){
            throw new NullPointerException();
        }
        //先去 redis 里找 在去库里找
        CoreConfig redis = getByConfigCodeRedis(configCode);
        if (ObjectUtils.isNotEmpty(redis)){
            return MapstructUtils.convert(redis, CoreConfigDto.class);
//            return ConvertUtil.convert(redis, CoreConfigDto.class);
        }
        CoreConfig one = getByConfigCodeSQL(configCode);
        if (ObjectUtils.isEmpty(one)){
            throw new NullPointerException("没有该变量 或者 变量禁用");
        }else {
            //塞到 redis 里;
            redisUtils.hset(redisCoreConfig,one.getConfigCode(), JSONObject.toJSONString(one));
        }
        return MapstructUtils.convert(one, CoreConfigDto.class);
//        return ConvertUtil.convert(one, CoreConfigDto.class);
    }


    private CoreConfig getByConfigCodeRedis(String configCode){
        Object hget = redisUtils.hget(redisCoreConfig, configCode);
        if (ObjectUtils.isEmpty(hget)){
            return null;
        }
        String toString = hget.toString();
        CoreConfig convert = JSONObject.parseObject(toString, CoreConfig.class);
        return convert;
    }


    private CoreConfig getByConfigCodeSQL(String configCode){
        QueryWrapper<CoreConfig> wrapper = new QueryWrapper<CoreConfig>()
            .eq("config_code",configCode)
            .eq("config_enabled","true")
            ;
        CoreConfig one = this.getOne(wrapper);
        return one;
    }
}
